package com.yy.hi.library.hilog;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.yy.hi.library.R;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * User: gongyongfeng2@yy.com
 * Date: 2020/6/11 21 :37
 */
public class HiViewPrinter implements HiLogPrint {

    private RecyclerView mRecyclerView;
    private LogAdapter mLogAdapter;
    private HiViewPrinterProvider mHiViewPrinterProvider;

    public HiViewPrinter(Activity activity) {
        FrameLayout rootView = activity.findViewById(android.R.id.content);
        mRecyclerView = new RecyclerView(activity);
        mLogAdapter = new LogAdapter();
        LinearLayoutManager layoutManager = new LinearLayoutManager(mRecyclerView.getContext());
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(mLogAdapter);
        mHiViewPrinterProvider = new HiViewPrinterProvider(rootView, mRecyclerView);
    }

    public HiViewPrinterProvider getHiViewPrinterProvider() {
        return mHiViewPrinterProvider;
    }

    @Override
    public void print(@NotNull HiLogConfig config, int level, String tag,
                      @NotNull String printString) {
        mLogAdapter.addItem(new HiLogMo(System.currentTimeMillis(), level, tag, printString));
        mRecyclerView.smoothScrollToPosition(mLogAdapter.getItemCount() - 1);
    }

    private static class LogAdapter extends RecyclerView.Adapter<LogAdapter.LogViewHolder> {
        private List<HiLogMo> logs = new ArrayList<>();

        public void addItem(HiLogMo hiLogMo) {
            logs.add(hiLogMo);
            notifyItemInserted(logs.size() - 1);
        }

        @NonNull
        @Override
        public LogViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.hi_log_item
                    , parent, false);
            return new LogViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(@NonNull LogViewHolder holder, int position) {
            HiLogMo mo = logs.get(position);
            int color = getHighColor(mo.level);
            holder.mTextTag.setTextColor(color);
            holder.mTextMessage.setTextColor(color);

            holder.mTextTag.setText(mo.getFlattened());
            holder.mTextMessage.setText(mo.log);
        }

        @Override
        public int getItemCount() {
            return logs.size();
        }

        private int getHighColor(int logLevel) {
            int highLight;
            switch (logLevel) {
                case HiLogType.V:
                    highLight = 0xffbbbbbb;
                    break;
                case HiLogType.D:
                    highLight = 0xffffffff;
                    break;
                case HiLogType.I:
                    highLight = 0xff6a8759;
                    break;
                case HiLogType.W:
                    highLight = 0xffbbb529;
                    break;
                case HiLogType.E:
                    highLight = 0xffff6b68;
                    break;
                default:
                    highLight = 0xffffff00;
                    break;
            }
            return highLight;
        }

        private static class LogViewHolder extends RecyclerView.ViewHolder {
            TextView mTextTag;
            TextView mTextMessage;

            public LogViewHolder(@NonNull View itemView) {
                super(itemView);
                mTextTag = itemView.findViewById(R.id.tag);
                mTextMessage = itemView.findViewById(R.id.message);
            }
        }
    }
}
